package com.teach.listener;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import org.apache.catalina.SessionEvent;
import org.apache.catalina.SessionListener;

import com.teach.service.ConfigService;
import com.teach.service.impl.CondigServiceImpl;

@WebListener
public class SystemListener implements HttpSessionListener,ServletContextListener{
	private int count = 0;
	private int online = 0;
	
	private ServletContext application;
	private ConfigService service = new CondigServiceImpl();
	
	@Override
	public void contextInitialized(ServletContextEvent sce) {
		//读取之前保存的访问数量
				count = service.getCount();
				
				application = sce.getServletContext();
				application.setAttribute("count", count);
				application.setAttribute("online", online);
		
	}

	
	public void sessionCreated(HttpSessionEvent se) {
		count ++;
		online ++;
		application.setAttribute("count", count);
		application.setAttribute("online", online);
	}


	public void sessionDestroyed(HttpSessionEvent se) {
		online --;
		application.setAttribute("online", online);
	}
	
	@Override
	public void contextDestroyed(ServletContextEvent sce) {
		// 保存现在访问的人数
		service.saveCount(count);
	}

	

}
